/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.List;
import javax.persistence.EntityManager;
import sosrest.Calendarios;
import sosrest.Usuarios;

/**
 *
 * @author pablomosquera
 */
public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<T> findRecurrente(int recurrente) {
        
        /*javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setParameter(recurrente.toString(), entityClass);   
        return q.getResultList();*/
        boolean bool = false;
        if(recurrente == 1){bool = true;}
       return getEntityManager().createNamedQuery("Citas.findByRecurrente").setParameter("recurrente", bool).getResultList();
        
        //Query q = cq.createNamedQuery("Citas.findByRecurrente").setParameter("recurrente", recurrente);
        //return q.getResultList();
    }
    
        public List<T> findPublico() {
        
        /*javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setParameter(recurrente.toString(), entityClass);   
        return q.getResultList();*/
        boolean bool = true;
        return getEntityManager().createNamedQuery("Calendarios.findByTipo").setParameter("tipo", bool).getResultList();
        
        //Query q = cq.createNamedQuery("Citas.findByRecurrente").setParameter("recurrente", recurrente);
        //return q.getResultList();
    }
        
        public List<T> findPublicoId(int id) {
        boolean bool = true;
        javax.persistence.Query q = getEntityManager().createNamedQuery("Calendarios.findByTipoAndId");
        q.setParameter("tipo", bool);
        q.setParameter("idUsuariofk", new Usuarios(id));
        return q.getResultList();
    }
        
        public List<T> findPrivadoId(int id) {
        boolean bool = false;
        javax.persistence.Query q = getEntityManager().createNamedQuery("Calendarios.findByTipoAndId");
        q.setParameter("tipo", bool);
        q.setParameter("idUsuariofk", new Usuarios(id));
        return q.getResultList();
    }
        
        
        public List<T> findCitaByCalendario(int id) {
        javax.persistence.Query q = getEntityManager().createNamedQuery("Citas.findByIdCalendario");
        q.setParameter("idCalendariofk", new Calendarios(id));
        return q.getResultList();
    }
    
        public List<T> findCitaByUsuario(int id) {
        javax.persistence.Query q = getEntityManager().createNamedQuery("Citas.findByUsuario2");
        q.setParameter("idUsuariofk", new Usuarios(id));
        return q.getResultList();
    }
        
    

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
    
    
}
